#ifndef INC_UMLLINE_H
#define INC_UMLLINE_H

#include "UMLObject.h"

#include <vector>
#include <QPolygon>

class UMLLine : public UMLObject
{
	public:
		typedef std::vector<QPoint>::size_type size_type;

		static const size_type NO_VERTEX = 0xFFFFFFFF;

		/*** CONSTRUCTORS ***/

		UMLLine(const QPoint& p1, const QPoint& p2, const QString& text);

		UMLLine(const std::vector<QPoint>& vertices,
			   	const BUMLSText& text = BUMLSText());

		// see note in UMLObject.h
		UMLLine& operator=(const UMLObject&);

		UMLLine* clone() const;

		void accept(DiagramVisitor&);

		/*** ACCESSORS ***/

		QPoint position() const;

		void setPosition(const QPoint&);

		QRect rect() const;

		/**
		 * Sets the endpoint of the line.
		 */
		void setEnd(const QPoint&);
		
		/**
		 * @return the number of vertices in this line. This will be >=2.
		 */
		size_type vertexCount() const;

		/**
		 * @return vertex at specified index
		 */
		QPoint vertex(size_type index) const;

		/**
		 * @return index of vertex at specified point, or NO_VERTEX if the point
		 * does not represent a vertex
		 */
		size_type vertex(const QPoint& pt) const;

		/**
		 * Sets replaces the vertex at the specified index with the given point.
		 *
		 * Complexity: O(n), average constant
		 */
		void setVertex(int index, const QPoint&);

		/**
		 * Inserts a vertex at the specified index, pushing [index,end) back.
		 *
		 * Complexity: O(n)
		 */
		void insertVertex(int index, const QPoint&);

		/**
		 * @return If pt lies on any line segment, returns the index of the
		 * first vertex of that segment (the second vertex is the index + 1). If
		 * not, returns NO_VERTEX.
		 */
		size_type segment(const QPoint& pt) const;

		/**
		 * @return an internally-cached polygon representing the whole line
		 */
		const QPolygon& polyline() const;

	private:

		void updateSize();

		std::vector<QPoint> _vertices;

		QRect _rectCache;

		typedef std::vector<QPoint>::iterator iterator;
		typedef std::vector<QPoint>::const_iterator const_iterator;
};

#endif // INC_UMLLINE_H

